﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace XPathQueryTester
{
    public partial class Form1 : Form
    {
        string m_AppName = "XPath Query Evaluator";
        Color m_FormBackgroundColor = Color.FromArgb(240, 240, 240);

        public Form1()
        {
            InitializeComponent();

            tableLayoutPanel2.Visible = false;
            
            this.Text = m_AppName;
            this.BackColor = m_FormBackgroundColor;

            richTextBox1.Font = new System.Drawing.Font("Lucida Console", 10);

            textBox1.Text = "/";
            button1.Enabled = false;
            button1.BackColor = m_FormBackgroundColor;

            contextMenuStrip1.Items.Add("/");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length > 1 && File.Exists(args[1]))
            {
                LoadXmlFile(args[1]);
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            comboBox1.Focus();
        }

        XmlDocument m_XmlDoc = new XmlDocument();
        private void LoadXmlFile(string filePath)
        {
            button1.Enabled = true;
            button1.BackColor = Color.White;

            try
            {
                //m_CurrentFile = filePath;

                string filename = Path.GetFileName(filePath);
                this.Text = m_AppName + " - " + filename;

                string Xml = File.ReadAllText(filePath);

                m_XmlDoc.LoadXml(Xml);
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error: " + ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                richTextBox1.Clear();

                string XPathQuery = textBox1.Text;
                contextMenuStrip1.Items.Add(XPathQuery);
                
                XmlNodeList nodes = m_XmlDoc.SelectNodes(XPathQuery);

                foreach (XmlNode node in nodes)
                {
                    richTextBox1.AppendText(Utils.FormatXml(node.OuterXml));
                    richTextBox1.AppendText(Environment.NewLine + Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error: " + ex.Message);
            }
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = ((string[])e.Data.GetData(DataFormats.FileDrop));
            bool first = true;
            foreach (string file in files)
            {
                if (first)
                {
                    LoadXmlFile(file);
                }
                first = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Point location = new Point(button2.Location.X, button2.Location.Y + button2.Height);
            contextMenuStrip1.Show(button2.Parent, location);
        }

        private void button2_MouseEnter(object sender, EventArgs e)
        {
            Point location = new Point(button2.Location.X, button2.Location.Y + button2.Height - 1);
            contextMenuStrip1.Show(button2.Parent, location);
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            contextMenuStrip1.Hide();
        }

        private void contextMenuStrip1_MouseEnter(object sender, EventArgs e)
        {
            bool test = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ProcessComboBoxValue();
        }

        void ProcessComboBoxValue()
        {
            try
            {
                richTextBox1.Clear();

                string XPathQuery = comboBox1.Text;

                comboBox1.SuspendLayout();
                for (int i = 0; i < comboBox1.Items.Count; i++)
                {
                    object item = comboBox1.Items[i];
                    if (item is string)
                    {
                        string strItem = item as string;
                        if (strItem == XPathQuery)
                        {
                            comboBox1.Items.RemoveAt(i);
                            break;
                        }
                    }
                }
                comboBox1.Items.Insert(0, XPathQuery);
                //comboBox1.SelectedIndex = 0;
                comboBox1.ResumeLayout();

                XmlNodeList nodes = m_XmlDoc.SelectNodes(XPathQuery);

                foreach (XmlNode node in nodes)
                {
                    richTextBox1.AppendText(Utils.FormatXml(node.OuterXml));
                    richTextBox1.AppendText(Environment.NewLine + Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error: " + ex.Message);
            }
        }

        private void comboBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ProcessComboBoxValue();
            }
        }
    }
}
